package fms.webservice;


import fms.xml.AutoPosResponseXML;
import fms.xml.PosRequestXML;
import fms.xml.PosResponseXML;
import fms.xml.WindResponseXML;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 14-7-8
 * Time: 下午4:44
 * To change this template use File | Settings | File Templates.
 */
public class WindService {
// http://localhost:8080/services/FMLWebService?wsdl
    public static WindResponseXML getWind(Request request) {
        FMLWebService service = new FMLWebService_Service().getFMLWebServicePort();
        fms.webservice.Response response =  service.getWind(request);
        WindResponseXML windResponseXML = XmlUtil.unMarshallWindResponse(response.getResponseXML());
        return windResponseXML;
    }

    public static PosResponseXML getPosition(Request request) {
        FMLWebService service = new FMLWebService_Service().getFMLWebServicePort();
        fms.webservice.Response response =  service.getPosition(request);
        PosResponseXML posResponseXML = XmlUtil.unMarshallPosResponse(response.getResponseXML());
        return posResponseXML;
    }

    public static AutoPosResponseXML getAutoPosition(Request request) {
        FMLWebService service = new FMLWebService_Service().getFMLWebServicePort();
        Response response =  service.getAutoPos(request);
        AutoPosResponseXML autoPosResponseXML = XmlUtil.unMarshallAutoPosResponse(response.getResponseXML());
        return autoPosResponseXML;
    }

    public static void main(String[] argv) {
        FMLWebService service = new FMLWebService_Service().getFMLWebServicePort();
        Request request = new Request();
        // 下面的服务类型与ServiceType中的枚举值关联
        request.setVersion("1.0");
//
//        /*位置报*/
        request.setServiceType("1");
        PosRequestXML reqPosXml = new PosRequestXML();
        reqPosXml.setCptname("TAO");
        reqPosXml.setAircraftid("B-6619");
        reqPosXml.setAirlines("");
        reqPosXml.setUsername("admin");
        reqPosXml.setPassword("password");
        request.setRequestXML(XmlUtil.marshallPosRequest(reqPosXml));
//
//        System.out.println("====\n\n" + XmlUtil.marshallPosRequest(reqPosXml));
//        Response response =  service.getPosition(request);
//
//
//        System.out.println("，，，，，" + response.getServiceType());
//        System.out.println(response.getVersion());
//        PosResponseXML posResponseXML = XmlUtil.unMarshallPosResponse(response.getResponseXML());
//        System.out.println(posResponseXML.getResultcode());
////        System.out.println("====\n\n" + XmlUtil.marshallResponse(response));

//        String posReqXML= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
//                "<request>\n" +
//                "    <requestXML>&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?&gt;\n" +
//                "&lt;posRequestInnerXML&gt;\n" +
//                "    &lt;airlines&gt;&lt;/airlines&gt;\n" +
//                "    &lt;aircraftid&gt;B-6619&lt;/aircraftid&gt;\n" +
//                "    &lt;cptname&gt;TAO&lt;/cptname&gt;\n" +
//                "&lt;/posRequestInnerXML&gt;\n" +
//                "</requestXML>\n" +
//                "    <serviceType>1</serviceType>\n" +
//                "    <version>1.0</version>\n" +
//                "</request>";


        String posReqXML= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                "<request>\n" +
                "    <requestXML>44</requestXML>\n" +
                "    <serviceType>1</serviceType>\n" +
                "    <version>1.0</version>\n" +
                "</request>";
       try{
            JAXBContext context = JAXBContext.newInstance(Request.class);
            Unmarshaller um =context.createUnmarshaller();
            StringReader reader = new StringReader(posReqXML);
            Request testReq = (Request)um.unmarshal(reader);
            System.out.println(testReq.getRequestXML());
        } catch (Exception e) {
            e.printStackTrace();
        }



/*        "<posRequestInnerXML>\n" +
                "    <airlines></airlines>\n" +
                "    <aircraftid>B-6619</aircraftid>\n" +
                "    <cptname>TAO</cptname>\n" +
                "</posRequestInnerXML>\n" +
                "</requestXML>\n" +*/


//        /*风温*/
//        WindRequestXML reqWindXml = new WindRequestXML();
//        request.setServiceType("0");
//        reqWindXml.setAlt("39000");
//        reqWindXml.setCptname("TAO");
//        reqWindXml.setAircraftid("B-6619");
//        reqWindXml.setFlightID("HO1161");
//        reqWindXml.setRequestdate("2014-05-20");
//        reqWindXml.setUsername("admin");
//        reqWindXml.setPassword("password");
//        request.setRequestXML(XmlUtil.marshallWindRequest(reqWindXml));
//
//        Response response =  service.getWind(request);
////
////        System.out.println(response.getServiceType());
////        System.out.println(response.getVersion());
//        WindResponseXML windResponseXML = XmlUtil.unMarshallWindResponse(response.getResponseXML());
//        System.out.println(windResponseXML.getCounts());
//        System.out.println(windResponseXML.getDatas().size());
//        System.out.println(windResponseXML.getResultcode());


/*飞机自动下发位置报请求*/
/*

2014-07-21 03:23:47
QU SHAUO9C
.BJSXCXA 170707
DEP
FI 9C8554/AN B-9920/DA ZSYA/DS ZSYA/OF 0706
DT BJS NKG 170707 M78A
-  0531FOB 264,

* */



//        request.setServiceType("2");
//        AutoPosRequestXML autoPosRequestXML = new AutoPosRequestXML();
//        autoPosRequestXML.setFlightID("HO1161");
//        autoPosRequestXML.setAircraftid("B-6619");
//        autoPosRequestXML.setRequestdate("2014-05-20");
//        request.setRequestXML(XmlUtil.marshallAutoPosRequest(autoPosRequestXML));
//        System.out.println("---------"+XmlUtil.marshallAutoPosRequest(autoPosRequestXML));
//        Response response =  service.getAutoPos(request);
//
//        System.out.println(response.getResponseXML());
//        System.out.println(response.getServiceType());
//        System.out.println(response.getVersion());

//          序列化和反序列化函数测试
//        try {
//            JAXBContext context = JAXBContext.newInstance(PosRequestInnerXML.class);
//            Marshaller ms =context.createMarshaller();
//            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//            //ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
//            //ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
//            StringWriter writer = new StringWriter();
//            //writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
//            ms.marshal(request,writer);
//            return writer.toString();
//
//            JAXBContext context = JAXBContext.newInstance(PosRequestInnerXML.class);
//            Marshaller ms =context.createMarshaller();
//            ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//            ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
////            ms.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
//            StringWriter writer = new StringWriter();
////            writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
//            ms.marshal(windResponseXML,writer);
//            System.out.println(writer.toString());
//
//            Unmarshaller unmarshaller =context.createUnmarshaller();
//            StringReader reader = new StringReader(writer.toString());
//            PosRequestInnerXML windResponseXML1 = (PosRequestInnerXML) unmarshaller.unmarshal(reader);
//            System.out.println(response.getServiceType());
//            System.out.println(response.getVersion());
//            System.out.println(windResponseXML1.getCounts());
//            System.out.println(windResponseXML1.getDatas().size());
//            System.out.println(windResponseXML1.getResultcode());
//        } catch (JAXBException e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }
    }
}
